package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangyao.dao.IAdminUserDao;
import com.jiangyao.dao.IAdminUserOrgClassDao;
import com.jiangyao.model.entity.AdminUser;
import com.jiangyao.model.entity.AdminUserOrgClass;
import com.jiangyao.service.AdminUserOrgClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 后台用户、组织、班级关系表 服务实现类
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-12
 */
@Service
public class AdminUserOrgClassServiceImpl extends ServiceImpl<IAdminUserOrgClassDao, AdminUserOrgClass> implements AdminUserOrgClassService {

	@Autowired
	private IAdminUserDao adminUserDao;

	@Override
	public Integer getAdminUserId(Integer orgId, Integer classId) {
		if (orgId == null && classId == null) {
			return null;
		}
		QueryWrapper<AdminUserOrgClass> queryWrapper = new QueryWrapper<>();
		if (orgId != null) {
			queryWrapper.eq("org_id", orgId);
		}
		if (classId != null) {
			queryWrapper.eq("class_id", classId);
		}
		queryWrapper.orderByDesc("create_time");
		queryWrapper.last(" LIMIT 1");
		AdminUserOrgClass adminUserOrgClass = this.getOne(queryWrapper);
		return adminUserOrgClass == null ? null : adminUserOrgClass.getAdminId();
	}

	@Override
	public AdminUser getAdminUser(Integer orgId, Integer classId) {
		Integer adminUserId = getAdminUserId(orgId, classId);
		if (adminUserId == null) {
			return null;
		}
		return adminUserDao.selectById(adminUserId);
	}
}
